package com.lucaswangdev.myswitch.example;

import com.lucaswangdev.myswitch.MySwitch;
import com.lucaswangdev.myswitch.annotation.DynamicConfig;
import com.lucaswangdev.myswitch.core.ConfigManager;

import java.lang.reflect.Field;

/**
 * 示例应用，演示如何使用MySwitch SDK
 */
public class ExampleApp {
    // 使用@DynamicConfig注解标记需要动态更新的字段
    @DynamicConfig(key = "feature.enabled", defaultValue = "false")
    private static boolean featureEnabled;
    
    @DynamicConfig(key = "timeout", defaultValue = "3000")
    private static int timeout;
    
    @DynamicConfig(key = "message", defaultValue = "Hello World")
    private static String message;
    
    public static void main(String[] args) throws InterruptedException, NoSuchFieldException {
        System.out.println("Starting MySwitch example app");
        
        // 初始化MySwitch SDK
        MySwitch.init("http://localhost:8080", "test-app", "default");
        System.out.println("MySwitch initialized");
        
        // 注册需要动态更新的静态字段
        ConfigManager configManager = ConfigManager.getInstance();
        
        // 注册featureEnabled字段
        Field featureEnabledField = ExampleApp.class.getDeclaredField("featureEnabled");
        configManager.registerDynamicField(null, featureEnabledField); // null for static fields
        
        // 注册timeout字段
        Field timeoutField = ExampleApp.class.getDeclaredField("timeout");
        configManager.registerDynamicField(null, timeoutField);
        
        // 注册message字段
        Field messageField = ExampleApp.class.getDeclaredField("message");
        configManager.registerDynamicField(null, messageField);
        
        System.out.println("Fields registered");
        
        // 显示初始配置
        System.out.println("Initial configuration:");
        System.out.println("Feature enabled: " + featureEnabled);
        System.out.println("Timeout: " + timeout);
        System.out.println("Message: " + message);
        
        // 模拟运行时配置更新
        System.out.println("\nUpdating configuration...");
        MySwitch.updateConfig("feature.enabled", "true");
        MySwitch.updateConfig("timeout", "10000");
        MySwitch.updateConfig("message", "Hello MySwitch");
        
        // 等待一段时间让更新生效
        Thread.sleep(1000);
        
        // 显示更新后的配置
        System.out.println("\nUpdated configuration:");
        System.out.println("Feature enabled: " + featureEnabled);
        System.out.println("Timeout: " + timeout);
        System.out.println("Message: " + message);
    }
}